﻿

Namespace YahooManaged.Finance.Screener.StockCriterias

    ''' <summary>
    ''' Criteria definition for current price (LastTradePriceOnly)
    ''' </summary>
    ''' <remarks></remarks>
    Public Class CurrentPriceCriteria
        Inherits StockDigitCriteriaDefinition

        Public Overrides ReadOnly Property DisplayName As String
            Get
                Return "Current Price Criteria"
            End Get
        End Property

        Public Overrides ReadOnly Property CriteriaName As String
            Get
                Return "Price"
            End Get
        End Property

        Public Overrides ReadOnly Property CriteriaGroup As StockScreenerCriteriaGroup
            Get
                Return StockScreenerCriteriaGroup.SharePerformance
            End Get
        End Property
        Public Overrides ReadOnly Property ProvidedQuoteProperties As QuoteProperty()
            Get
                Return {QuoteProperty.Symbol, _
                        QuoteProperty.Name, _
                        QuoteProperty.LastTradePriceOnly, _
                        QuoteProperty.LastTradeTime, _
                        QuoteProperty.MarketCapitalization}
            End Get
        End Property
        Public Overrides ReadOnly Property ProvidedScreenerProperties As StockScreenerProperty()
            Get
                Return {StockScreenerProperty.ReturnOnEquity, _
                        StockScreenerProperty.ReturnOnAssets, _
                        StockScreenerProperty.ForwardPriceToEarningsRatio}
            End Get
        End Property

        Public Sub New()
            MyBase.New("b"c)
        End Sub

    End Class

    ''' <summary>
    ''' Criteria definition for price change gainer or losers
    ''' </summary>
    ''' <remarks></remarks>
    Public Class PriceGainerLosersCriteria
        Inherits StockDigitCriteriaDefinition
        Implements IPercentageAvailability
        Implements IGainLoss
        Implements IAbsoluteTimePoint

        Public Overrides ReadOnly Property DisplayName As String
            Get
                Return "Price Gainer/Losers"
            End Get
        End Property

        Public Overrides ReadOnly Property CriteriaName As String
            Get
                Return "Price"
            End Get
        End Property

        Private mRelativeDate As StockTradingAbsoluteTimePoint = StockTradingAbsoluteTimePoint.TodaysOpen
        Private mGainOrLoss As StockPriceChangeDirection = StockPriceChangeDirection.Gain

        Public Overrides ReadOnly Property CriteriaGroup As StockScreenerCriteriaGroup
            Get
                Return StockScreenerCriteriaGroup.SharePerformance
            End Get
        End Property
        Public Overrides ReadOnly Property ProvidedQuoteProperties As QuoteProperty()
            Get
                If Me.ValueRelativeTo = StockTradingAbsoluteTimePoint.TodaysOpen Then
                    Return {QuoteProperty.Symbol, _
                            QuoteProperty.Name, _
                            QuoteProperty.LastTradePriceOnly, _
                            QuoteProperty.LastTradeTime, _
                            QuoteProperty.MarketCapitalization, _
                            QuoteProperty.Change, _
                            QuoteProperty.ChangeInPercent, _
                            QuoteProperty.Open}
                Else
                    Return {QuoteProperty.Symbol, _
                            QuoteProperty.Name, _
                            QuoteProperty.LastTradePriceOnly, _
                            QuoteProperty.LastTradeTime, _
                            QuoteProperty.MarketCapitalization, _
                            QuoteProperty.Change, _
                            QuoteProperty.ChangeInPercent, _
                            QuoteProperty.PreviousClose}

                End If
            End Get
        End Property
        Public Overrides ReadOnly Property ProvidedScreenerProperties As StockScreenerProperty()
            Get
                Return {StockScreenerProperty.ReturnOnEquity, _
                        StockScreenerProperty.ReturnOnAssets, _
                        StockScreenerProperty.ForwardPriceToEarningsRatio}
            End Get
        End Property

        Public Property ValueRelativeTo As StockTradingAbsoluteTimePoint Implements IAbsoluteTimePoint.ValueRelativeTo
            Get
                Return mRelativeDate
            End Get
            Set(ByVal value As StockTradingAbsoluteTimePoint)
                mRelativeDate = value
                Me.SetOptionalParam()
            End Set
        End Property
        Public Property GainOrLoss As StockPriceChangeDirection Implements IGainLoss.GainOrLoss
            Get
                Return mGainOrLoss
            End Get
            Set(ByVal value As StockPriceChangeDirection)
                mGainOrLoss = value
                Me.SetOptionalParam()
            End Set
        End Property
        Public Property PercentValues As Boolean Implements IPercentageAvailability.PercentValues
            Get
                Return CBool(IIf(MyBase.CriteriaTag = "g", True, False))
            End Get
            Set(ByVal value As Boolean)
                If value Then : MyBase.CriteriaTag = "g"
                Else : MyBase.CriteriaTag = "f"
                End If
            End Set
        End Property

        Private Sub SetOptionalParam()
            If mGainOrLoss = StockPriceChangeDirection.Gain Then
                If mRelativeDate = StockTradingAbsoluteTimePoint.TodaysOpen Then : MyBase.OptionalParamValue = 0
                Else : MyBase.OptionalParamValue = 1
                End If
            Else
                If mRelativeDate = StockTradingAbsoluteTimePoint.TodaysOpen Then : MyBase.OptionalParamValue = 2
                Else : MyBase.OptionalParamValue = 3
                End If
            End If
        End Sub

        Public Sub New()
            MyBase.New("f"c)
            Me.SetOptionalParam()
        End Sub

    End Class

    ''' <summary>
    ''' Criteria definition for price development in a specific time period
    ''' </summary>
    ''' <remarks></remarks>
    Public Class PriceMomentumCriteria
        Inherits StockDigitCriteriaDefinition
        Implements IPercentageAvailability
        Implements IGainLoss
        Implements IRelativeTimePoint
        Implements IRelativeTimeSpan

        Public Overrides ReadOnly Property DisplayName As String
            Get
                Return "Price Momentum Criteria"
            End Get
        End Property

        Public Overrides ReadOnly Property CriteriaName As String
            Get
                Return "Price"
            End Get
        End Property

        Private mRelativeDate As StockTradingRelativeTimePoint = StockTradingRelativeTimePoint.BeforeLastTradeTimePoint
        Private mGainOrLoss As StockPriceChangeDirection = StockPriceChangeDirection.Gain
        Private mTimeSpanInMinutes As StockTradingTimeSpan = StockTradingTimeSpan._1

        Public Overrides ReadOnly Property CriteriaGroup As StockScreenerCriteriaGroup
            Get
                Return StockScreenerCriteriaGroup.SharePerformance
            End Get
        End Property
        Public Overrides ReadOnly Property ProvidedQuoteProperties As QuoteProperty()
            Get
                Return {QuoteProperty.Symbol, _
                        QuoteProperty.Name, _
                        QuoteProperty.LastTradePriceOnly, _
                        QuoteProperty.LastTradeTime, _
                        QuoteProperty.MarketCapitalization}
            End Get
        End Property
        Public Overrides ReadOnly Property ProvidedScreenerProperties As StockScreenerProperty()
            Get
                Return {StockScreenerProperty.ReturnOnEquity, _
                        StockScreenerProperty.ReturnOnAssets, _
                        StockScreenerProperty.ForwardPriceToEarningsRatio}
            End Get
        End Property

        Public Property TimeSpanRelativeTo As StockTradingRelativeTimePoint Implements IRelativeTimePoint.TimeSpanRelativeTo
            Get
                Return mRelativeDate
            End Get
            Set(ByVal value As StockTradingRelativeTimePoint)
                mRelativeDate = value
                Me.SetOptionalParam()
            End Set
        End Property
        Public Property GainOrLoss As StockPriceChangeDirection Implements IGainLoss.GainOrLoss
            Get
                Return mGainOrLoss
            End Get
            Set(ByVal value As StockPriceChangeDirection)
                mGainOrLoss = value
                Me.SetOptionalParam()
            End Set
        End Property
        Public Property RelativeTimeSpanInMinutes As StockTradingTimeSpan Implements IRelativeTimeSpan.RelativeTimeSpanInMinutes
            Get
                Return mTimeSpanInMinutes
            End Get
            Set(ByVal value As StockTradingTimeSpan)
                mTimeSpanInMinutes = value
                Me.SetOptionalParam()
            End Set
        End Property
        Public Property PercentValues As Boolean Implements IPercentageAvailability.PercentValues
            Get
                Return CBool(IIf(MyBase.CriteriaTag = "i", True, False))
            End Get
            Set(ByVal value As Boolean)
                If value Then : MyBase.CriteriaTag = "i"
                Else : MyBase.CriteriaTag = "h"
                End If
            End Set
        End Property

        Private Sub SetOptionalParam()
            MyBase.OptionalParamValue = (CInt(mGainOrLoss) * 12) + (CInt(mRelativeDate) * 6) + CInt(mTimeSpanInMinutes)
        End Sub

        Public Sub New()
            MyBase.New("h"c)
            Me.SetOptionalParam()
        End Sub

    End Class

    ''' <summary>
    ''' Criteria definition for extreme price value and current price
    ''' </summary>
    ''' <remarks></remarks>
    Public Class ExtremePriceCriteria
        Inherits StockDigitCriteriaDefinition
        Implements IPercentageAvailability
        Implements IExtremeParameter
        Implements ILessGreater

        Public Overrides ReadOnly Property DisplayName As String
            Get
                Return "Extreme Price Criteria"
            End Get
        End Property

        Public Overrides ReadOnly Property CriteriaName As String
            Get
                Return "Price"
            End Get
        End Property

        Private mExtremeParameter As StockExtremeParameter = StockExtremeParameter.TodaysHigh
        Private mLessGreater As LessGreater = LessGreater.Less

        Public Overrides ReadOnly Property CriteriaGroup As StockScreenerCriteriaGroup
            Get
                Return StockScreenerCriteriaGroup.SharePerformance
            End Get
        End Property
        Public Overrides ReadOnly Property ProvidedQuoteProperties As QuoteProperty()
            Get
                Select Case Me.ExtremeParameter
                    Case StockExtremeParameter.TodaysHigh
                        Return {QuoteProperty.Symbol, _
                                QuoteProperty.Name, _
                                QuoteProperty.LastTradePriceOnly, _
                                QuoteProperty.LastTradeTime, _
                                QuoteProperty.MarketCapitalization, _
                                QuoteProperty.DaysHigh}

                    Case StockExtremeParameter.TodaysLow
                        Return {QuoteProperty.Symbol, _
                                QuoteProperty.Name, _
                                QuoteProperty.LastTradePriceOnly, _
                                QuoteProperty.LastTradeTime, _
                                QuoteProperty.MarketCapitalization, _
                                QuoteProperty.DaysLow}

                    Case StockExtremeParameter.YearsHigh
                        Return {QuoteProperty.Symbol, _
                                QuoteProperty.Name, _
                                QuoteProperty.LastTradePriceOnly, _
                                QuoteProperty.LastTradeTime, _
                                QuoteProperty.MarketCapitalization, _
                                QuoteProperty.YearHigh, _
                                QuoteProperty.ChangeInPercentFromYearHigh, _
                                QuoteProperty.ChangeFromYearHigh}

                    Case StockExtremeParameter.YearsLow
                        Return {QuoteProperty.Symbol, _
                                QuoteProperty.Name, _
                                QuoteProperty.LastTradePriceOnly, _
                                QuoteProperty.LastTradeTime, _
                                QuoteProperty.MarketCapitalization, _
                                QuoteProperty.YearLow, _
                                QuoteProperty.PercentChangeFromYearLow, _
                                QuoteProperty.ChangeFromYearLow}
                    Case Else : Return New QuoteProperty() {}

                End Select
            End Get
        End Property
        Public Overrides ReadOnly Property ProvidedScreenerProperties As StockScreenerProperty()
            Get
                Return {StockScreenerProperty.ReturnOnEquity, _
                        StockScreenerProperty.ReturnOnAssets, _
                        StockScreenerProperty.ForwardPriceToEarningsRatio}
            End Get
        End Property

        Friend Overrides ReadOnly Property IsValid As Boolean
            Get
                Return MyBase.IsValid And Not ((mLessGreater = LessGreater.Greater And mExtremeParameter = StockExtremeParameter.TodaysHigh) Or (mLessGreater = LessGreater.Less And mExtremeParameter = StockExtremeParameter.TodaysLow))
            End Get
        End Property
        Public Property PercentValues As Boolean Implements IPercentageAvailability.PercentValues
            Get
                Return CBool(IIf(MyBase.CriteriaTag = "k", True, False))
            End Get
            Set(ByVal value As Boolean)
                If value Then : MyBase.CriteriaTag = "k"
                Else : MyBase.CriteriaTag = "j"
                End If
            End Set
        End Property

        ''' <summary>
        ''' With Today's High/Low only following combination is allowed: Less/TodaysHigh or Greater/TodaysLow. Other combinations wouldn't make sense.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property ExtremeParameter As StockExtremeParameter Implements IExtremeParameter.ExtremeParameter
            Get
                Return mExtremeParameter
            End Get
            Set(ByVal value As StockExtremeParameter)
                mExtremeParameter = value
                Me.SetOptionalParam()
            End Set
        End Property
        ''' <summary>
        ''' With Today's High/Low only following combination is allowed: Less/TodaysHigh or Greater/TodaysLow. Other combinations wouldn't make sense.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property LessGreater As LessGreater Implements ILessGreater.LessGreater
            Get
                Return mLessGreater
            End Get
            Set(ByVal value As LessGreater)
                mLessGreater = value
                Me.SetOptionalParam()
            End Set
        End Property

        Private Sub SetOptionalParam()
            Select Case mExtremeParameter
                Case StockExtremeParameter.TodaysHigh
                    MyBase.OptionalParamValue = 0
                Case StockExtremeParameter.TodaysLow
                    MyBase.OptionalParamValue = 1
                Case StockExtremeParameter.YearsHigh
                    If mLessGreater = LessGreater.Less Then : MyBase.OptionalParamValue = 2
                    Else : MyBase.OptionalParamValue = 4
                    End If
                Case StockExtremeParameter.YearsLow
                    If mLessGreater = LessGreater.Less Then : MyBase.OptionalParamValue = 5
                    Else : MyBase.OptionalParamValue = 3
                    End If
            End Select
        End Sub

        Public Sub New()
            MyBase.New("j"c)
            Me.SetOptionalParam()
        End Sub

    End Class

    ''' <summary>
    ''' Criteria definition for trading gaps
    ''' </summary>
    ''' <remarks></remarks>
    Public Class GapVsPreviousClose
        Inherits StockDigitCriteriaDefinition
        Implements IPercentageAvailability
        Implements IUpDown

        Public Overrides ReadOnly Property DisplayName As String
            Get
                Return "Gap vs. Previous Close"
            End Get
        End Property

        Public Overrides ReadOnly Property CriteriaName As String
            Get
                Return "Gap"
            End Get
        End Property

        Public Overrides ReadOnly Property CriteriaGroup As StockScreenerCriteriaGroup
            Get
                Return StockScreenerCriteriaGroup.SharePerformance
            End Get
        End Property
        Public Overrides ReadOnly Property ProvidedQuoteProperties As QuoteProperty()
            Get
                Return {QuoteProperty.Symbol, _
                        QuoteProperty.Name, _
                        QuoteProperty.LastTradePriceOnly, _
                        QuoteProperty.LastTradeTime, _
                        QuoteProperty.MarketCapitalization}
            End Get
        End Property
        Public Overrides ReadOnly Property ProvidedScreenerProperties As StockScreenerProperty()
            Get
                If Me.PercentValues Then
                    Return {StockScreenerProperty.ReturnOnEquity, _
                            StockScreenerProperty.ReturnOnAssets, _
                            StockScreenerProperty.ForwardPriceToEarningsRatio, _
                            StockScreenerProperty.GapInPercent}
                Else
                    Return {StockScreenerProperty.ReturnOnEquity, _
                            StockScreenerProperty.ReturnOnAssets, _
                            StockScreenerProperty.ForwardPriceToEarningsRatio, _
                            StockScreenerProperty.Gap}
                End If
            End Get
        End Property

        Public Property PercentValues As Boolean Implements IPercentageAvailability.PercentValues
            Get
                Return CBool(IIf(MyBase.CriteriaTag = "m", True, False))
            End Get
            Set(ByVal value As Boolean)
                If value Then : MyBase.CriteriaTag = "m"
                Else : MyBase.CriteriaTag = "l"
                End If
            End Set
        End Property

        Public Property UpDown As UpDown Implements IUpDown.UpDown
            Get
                Return CType(MyBase.OptionalParamValue, UpDown)
            End Get
            Set(ByVal value As UpDown)
                MyBase.OptionalParamValue = CInt(value)
            End Set
        End Property

        Public Sub New()
            MyBase.New("l"c)
            Me.UpDown = Screener.UpDown.Up
        End Sub
    End Class

    ''' <summary>
    ''' Criteria class for current price vs. Moving Average value in percent
    ''' </summary>
    ''' <remarks>Describes how much is the difference between Moving Average value (base) and last trade price (criteria value in percent)</remarks>
    Public Class PriceToMovingAverageRatioCriteria
        Inherits StockDigitCriteriaDefinition
        Implements IUpDown
        Implements IMovingAverage

        Public Overrides ReadOnly Property DisplayName As String
            Get
                Return "Price/Moving Average Ratio Criteria"
            End Get
        End Property

        Public Overrides ReadOnly Property CriteriaName As String
            Get
                Return "Price/MovingAverage"
            End Get
        End Property

        Private mUpDown As UpDown = UpDown.Up
        Private mMovingAverage As MovingAverageType = MovingAverageType.FiftyDays

        Public Overrides ReadOnly Property CriteriaGroup As StockScreenerCriteriaGroup
            Get
                Return StockScreenerCriteriaGroup.SharePerformance
            End Get
        End Property
        Public Overrides ReadOnly Property ProvidedQuoteProperties As QuoteProperty()
            Get
                If Me.MovingAverage = MovingAverageType.FiftyDays Then
                    Return {QuoteProperty.Symbol, _
                            QuoteProperty.Name, _
                            QuoteProperty.LastTradePriceOnly, _
                            QuoteProperty.LastTradeTime, _
                            QuoteProperty.MarketCapitalization, _
                            QuoteProperty.FiftydayMovingAverage, _
                            QuoteProperty.ChangeFromFiftydayMovingAverage, _
                            QuoteProperty.PercentChangeFromFiftydayMovingAverage}
                Else
                    Return {QuoteProperty.Symbol, _
                            QuoteProperty.Name, _
                            QuoteProperty.LastTradePriceOnly, _
                            QuoteProperty.LastTradeTime, _
                            QuoteProperty.MarketCapitalization, _
                            QuoteProperty.TwoHundreddayMovingAverage, _
                            QuoteProperty.ChangeFromTwoHundreddayMovingAverage, _
                            QuoteProperty.PercentChangeFromTwoHundreddayMovingAverage}
                End If

            End Get
        End Property
        Public Overrides ReadOnly Property ProvidedScreenerProperties As StockScreenerProperty()
            Get
                Return {StockScreenerProperty.ReturnOnEquity, _
                        StockScreenerProperty.ReturnOnAssets, _
                        StockScreenerProperty.ForwardPriceToEarningsRatio}
            End Get
        End Property

        Public Property UpDown As UpDown Implements IUpDown.UpDown
            Get
                Return mUpDown
            End Get
            Set(ByVal value As UpDown)
                mUpDown = value
                Me.SetOptionalParam()
            End Set
        End Property
        Public Property MovingAverage As MovingAverageType Implements IMovingAverage.MovingAverage
            Get

                Return mMovingAverage
            End Get
            Set(ByVal value As MovingAverageType)
                mMovingAverage = value
                Me.SetOptionalParam()
            End Set
        End Property

        Private Sub SetOptionalParam()
            If Me.UpDown = UpDown.Up Then
                If Me.MovingAverage = MovingAverageType.FiftyDays Then : MyBase.OptionalParamValue = 0
                Else : MyBase.OptionalParamValue = 1
                End If
            Else
                If Me.MovingAverage = MovingAverageType.FiftyDays Then : MyBase.OptionalParamValue = 2
                Else : MyBase.OptionalParamValue = 3
                End If
            End If
        End Sub

        Public Sub New()
            MyBase.New("o")
            Me.SetOptionalParam()
        End Sub


    End Class

End Namespace